using UnityEngine;
using System.Collections;

/**
 * Script of Replay reader.
 * The purpose of this class is to action level automatically with transform datas.
 * @author Benjamin Bruneau
 */
public class ReplayReaderScript : MonoBehaviour
{
	
	private const string REPLAY_MODE_NAME = "ReplayMode"; // Replay Mode GameObject's name
	
	private int _count = 0; // Action count
	private int _sequenceSize; // Total count of actions to re-play
	
	private ArrayList _sequence; // The replay in arraylist
	
	private GameObject _replayMode; // Replay mode GameObject
	
	public GameObject _embeddedCamera; // Game camera GameObject
	public GameObject _dataContainer; // Data container GameObject
	
	
	
	/**
	 * Called on initialization.
	 * @return void
	 */
	void Start ()
	{
	
		//Initialized because this object moves through scenes
		_replayMode = GameObject.Find (REPLAY_MODE_NAME);
		
		// If replay mode
		if (_replayMode.GetComponent<ReplayModeScript> ()._replayMode) 
		{			
			// Load replay and enable replay camera
			_sequence = this.gameObject.GetComponent<StreamWriterScript> ().read ();
			_sequenceSize = _sequence.Count;
			_embeddedCamera.GetComponent<ReplayCameraScript> ().enabled = true;
			
		} else {
			this.enabled = false;
		}
	}
	
	/**
	 * Called once per fixed frame.
	 * @return void
	 */
	void FixedUpdate ()
	{
	
		moveObject ();
			
	}
	
	/**
	 * Moves objects with transform component information and time.
	 * @return void
	 */
	private void moveObject ()
	{
		
		// Is multiple action on a unique frame
		bool continueOnThisFrame = false;
		
		// Do on a unique frame
		do {
			// If replay is not over
			if (_count != _sequenceSize) {
				
				float actualTime = _dataContainer.GetComponent<TimeScript> ().getTime ();
				float nextTime = (float)((ArrayList)_sequence [_count]) [8];
				
				// If replay time and actual time are equals
				if (!(actualTime < nextTime)) 
				{
					// Impacted Game Object
					GameObject impactedGameObject = GameObject.Find ((string)((ArrayList)_sequence [_count]) [0]);
					
					//Position to get
					Vector3 newPosition = new Vector3 ((float)((ArrayList)_sequence [_count]) [1], (float)((ArrayList)_sequence [_count]) [2], (float)((ArrayList)_sequence [_count]) [3]);
					impactedGameObject.transform.position = newPosition;
					
					// Rotation to get
					Quaternion newRotation = new Quaternion ((float)((ArrayList)_sequence [_count]) [4], (float)((ArrayList)_sequence [_count]) [5], (float)((ArrayList)_sequence [_count]) [6], (float)((ArrayList)_sequence [_count]) [7]);
					impactedGameObject.transform.rotation = newRotation;
					
					_count++;
					
					// If replay is not over
					if (_count != _sequenceSize) 
					{
						// Checks if next move is on this exact frame
						if (nextTime == (float)((ArrayList)_sequence [_count]) [8]) 
						{
							continueOnThisFrame = true;
						} else {
							continueOnThisFrame = false;
						}
					}
				}
			} else {
				continueOnThisFrame = false;
			}
		} while (continueOnThisFrame);
	}	
}